using System;
using System.Collections.Generic;
using UnityEngine;

public class MainUI : MonoBehaviour
{
	private GameObject uiobj;

	public UIEventListener rewardbtn;

	public UIEventListener storebtn;

	public UIEventListener battlebtn;

	public UIEventListener challengebtn;

	public UIEventListener moregamebtn;

	public UIEventListener lastbtn;

	public UIEventListener nextbtn;

	public UIEventListener salebtn;

	public UIEventListener dailybtn;

	public UIEventListener settingbtn;

	public UIEventListener boxbtn;

	public GameObject saleobj;// 礼包

	public UILabel saletimelabel;

	public UISprite lastsp;

	public UISprite nextsp;

	private int CurPlane;

	private int CurIndex;

	public List<int> Planelist;

	public UISprite[] points;

	public Transform selectpoint;

	public UILabel planelevellabel;

	public UILabel planenamelabel;

	public UISprite currankflag;

	public UILabel currankname;

	public UIEventListener junxianbtn;

    public UIEventListener privacybtn;

    public UISprite[] forstars;

	public TweenPosition downlineanima;

	public TweenAlpha midareaanima;

	public Transform downlinetra;

	public UIWidget midareawi;

	public UISprite moregamehint;

	public UISprite rewardhint;

	public UISprite challengehint;

	public UISprite dailyhint;

	public UISprite storehint;

	private bool showsaletime;

	private long sale_sarttime;

	private long curtime;

	private int intersec;

	public static int caltime;

	private float temptime;

	private int saletime = 172800;

	private bool isfirstshow = true;

	private int chahintid;

	private expdata curexpdata;

	private float clicktime;

	private bool clickflag;

	private void Start()
	{
		rewardbtn.onClick = OnClickrewardBtn;
		UIEventListener uIEventListener = storebtn;
		uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, new UIEventListener.VoidDelegate(OnClickstoreBtn));
		UIEventListener uIEventListener2 = battlebtn;
		uIEventListener2.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onClick, new UIEventListener.VoidDelegate(OnClickbattleBtn));
		challengebtn.onClick = OnClickchallengeBtn;
		lastbtn.onClick = OnClicklastBtn;
		nextbtn.onClick = OnClicknextBtn;
		junxianbtn.onClick = OnClickjunxianBtn;
		privacybtn.onClick = OnClickPrivacyBtn;
        moregamebtn.onClick = OnClickmoregameBtn;
		salebtn.onClick = OnClicksaleBtn;
		dailybtn.onClick = OnClickdailyBtn;
		settingbtn.onClick = OnClicksetBtn;
		boxbtn.onClick = OnClickboxbtn;
	}

	private void Update()
	{
		if (clickflag)
		{
			clicktime += Time.deltaTime;
			if (clicktime >= 1f)
			{
				clickflag = false;
				clicktime = 0f;
			}
		}
		if (!showsaletime)
		{
			return;
		}
		temptime += Time.deltaTime;
		if (!(temptime >= 1f))
		{
			return;
		}
		temptime = 0f;
		caltime--;
		saletimelabel.text = MyMath.longtimeset(caltime);
		if (caltime > 0)
		{
			return;
		}
		if (MenuPlayerprefs.instance.saleid == 0)
		{
			if (MenuPlayerprefs.instance.curgamelevel >= 17)
			{
				MenuPlayerprefs.instance.save_sale(2);
				//NGUITools.SetActive(saleobj, state: true);
				showsaletime = true;
				caltime = saletime;
			}
			else
			{
				MenuPlayerprefs.instance.save_sale(1);
				showsaletime = false;
				//NGUITools.SetActive(saleobj, state: false);
			}
		}
		else
		{
			MenuPlayerprefs.instance.save_sale(3);
			showsaletime = false;
			//NGUITools.SetActive(saleobj, state: false);
		}
	}

	public void init_ui_info()
	{
		personinfo_show();
		MenuPlayerprefs.instance.readStoreData();
		MenuPlayerprefs.instance.readstartplane();
		changePlane();
		Invoke("showhint", 0.3f);
		if (MenuPlayerprefs.instance.menufirst == 1)
		{
			isfirstshow = false;
			setfixedpos();
			MenuUIController.instance.enable_guideui(isone: true);
		}
		else if (isfirstshow)
		{
			Invoke("showAnima", 0.6f);
			isfirstshow = false;
		}
		show_saleui();
	}

	public void changePlane()
	{
		CurPlane = MenuPlayerprefs.instance.StartPlane;
		Planelist = new List<int>();
		Planelist = MenuPlayerprefs.instance.GetUnlockPlane();
		int num = 0;
		num = ((Planelist.Count % 2 != 0) ? (-24 * (Planelist.Count / 2)) : (-12 - 24 * ((Planelist.Count - 1) / 2)));
		for (int i = 0; i < points.Length; i++)
		{
			if (i < Planelist.Count)
			{
				points[i].transform.localPosition = new Vector3(num + 24 * i, 40f, 0f);
				points[i].enabled = true;
				if (CurPlane == Planelist[i])
				{
					selectpoint.parent = points[i].transform;
					selectpoint.localPosition = Vector3.zero;
					CurIndex = i;
				}
			}
			else
			{
				points[i].enabled = false;
			}
		}
		if (Planelist.Count > 1)
		{
			lastsp.enabled = true;
			nextsp.enabled = true;
		}
		else
		{
			lastsp.enabled = false;
			nextsp.enabled = false;
		}
		showplane();
	}

	public void show_saleui()
	{
		if (MenuPlayerprefs.instance.saleid == 0 || MenuPlayerprefs.instance.saleid == 2)
		{
			sale_sarttime = long.Parse(MenuPlayerprefs.instance.SaleStartTime);
			if (sale_sarttime == 1)
			{
				MenuPlayerprefs.instance.save_sale(0);
			}
			curtime = DateTime.Now.Ticks;
			intersec = (int)((curtime - sale_sarttime) / 10000000);
			if (intersec < 0)
			{
				intersec = 0;
			}
			caltime = saletime - intersec;
			if (caltime > 0)
			{
				//NGUITools.SetActive(saleobj, state: true);
				showsaletime = true;
			}
			else if (MenuPlayerprefs.instance.saleid == 0)
			{
				if (MenuPlayerprefs.instance.curgamelevel >= 17)
				{
					MenuPlayerprefs.instance.save_sale(2);
					//NGUITools.SetActive(saleobj, state: true);
					showsaletime = true;
					caltime = saletime;
				}
				else
				{
					MenuPlayerprefs.instance.save_sale(1);
					showsaletime = false;
					//NGUITools.SetActive(saleobj, state: false);
				}
			}
			else
			{
				MenuPlayerprefs.instance.save_sale(3);
				showsaletime = false;
				//NGUITools.SetActive(saleobj, state: false);
			}
		}
		else if (MenuPlayerprefs.instance.saleid == 1)
		{
			if (MenuPlayerprefs.instance.curgamelevel >= 17)
			{
				MenuPlayerprefs.instance.save_sale(2);
				//NGUITools.SetActive(saleobj, state: true);
				showsaletime = true;
				caltime = saletime;
			}
			else
			{
				showsaletime = false;
				//NGUITools.SetActive(saleobj, state: false);
			}
		}
		else
		{
			showsaletime = false;
			//NGUITools.SetActive(saleobj, state: false);
		}
	}

	public void showplane()
	{
		planelevellabel.text = string.Empty + MenuDataController.instance.PlaneData[CurPlane].level;
		planenamelabel.text = MenuDataController.instance.PlaneData[CurPlane].name;
		if (ShowController.instance != null)
		{
			ShowController.instance.showplane(CurPlane);
		}
		show_star();
	}

	public void show_star()
	{
		int num = (int)(MenuDataController.instance.PlaneData[CurPlane].star * 10f);
		int num2 = num / 10;
		bool flag = (num % 10 != 0) ? true : false;
		for (int i = 0; i < forstars.Length; i++)
		{
			if (i < num2)
			{
				forstars[i].spriteName = "JS_xing2";
				forstars[i].enabled = true;
			}
			else if (i == num2 && flag)
			{
				forstars[i].spriteName = "JS_banXing";
				forstars[i].enabled = true;
			}
			else
			{
				forstars[i].enabled = false;
			}
		}
	}

	public void showAnima()
	{
		MenuUIController.instance.uplineui.parentanimaPF();
		midareaanima.PlayForward();
		downlineanima.PlayForward();
	}

	public void setfixedpos()
	{
		MenuUIController.instance.uplineui.setfixedpos();
		midareawi.alpha = 1f; midareawi.gameObject.SetActive(true);
		downlinetra.localPosition = Vector3.zero;
	}

	public void showhint()
	{
		if (MenuBackController.instance.curstate == MenuUIState.mainui)
		{
			if (MenuPlayerprefs.instance.firstshowgame == 0)
			{
				moregamehint.enabled = true;
			}
			if (MenuUIController.instance.rewardui.GetHintinfo())
			{
				rewardhint.enabled = true;
			}
			chahintid = MenuUIController.instance.challengeui.Gethintinfo();
			if (chahintid != -1)
			{
				challengehint.enabled = true;
			}
			if (Platform.GetBonusCount() != -1)
			{
				dailyhint.enabled = true;
			}
			else
			{
				dailyhint.enabled = false;
			}
			if (MenuUIController.instance.garageui.show_hintflag(curexpdata.level))
			{
				storehint.enabled = true;
			}
			else
			{
				storehint.enabled = false;
			}
			if (MenuPlayerprefs.instance.popsaleflag == 1 && MenuPlayerprefs.instance.menufirst == 0)
			{
				OnClicksaleBtn(null);
				MenuPlayerprefs.instance.set_popsaleflag();
			}
		}
	}

	public void dis_dailyhint()
	{
		dailyhint.enabled = false;
	}

	public void personinfo_show()
	{
		curexpdata = MenuPlayerprefs.instance.GetExp();
		if (curexpdata.level >= 19)
		{
			curexpdata.level = 19;
			currankflag.spriteName = "19." + MenuPlayerprefs.instance.ranknameImage[curexpdata.level - 1];
			currankname.text = curexpdata.name;
			currankflag.SetDimensions(56, 48);
			return;
		}
		currankflag.spriteName = string.Empty + curexpdata.level + "." + MenuPlayerprefs.instance.ranknameImage[curexpdata.level - 1];
		currankname.text = curexpdata.name;
		if (curexpdata.level >= 15)
		{
			currankflag.SetDimensions(56, 48);
		}
		else
		{
			currankflag.SetDimensions(40, 44);
		}
	}

	public void OnClicklastBtn(GameObject btn)
	{
		if (!clickflag && Planelist.Count != 1)
		{
			clickflag = true;
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			CurIndex = (CurIndex - 1 + Planelist.Count) % Planelist.Count;
			CurPlane = Planelist[CurIndex];
			selectpoint.parent = points[CurIndex].transform;
			selectpoint.localPosition = Vector3.zero;
			showplane();
		}
	}

	public void OnClicknextBtn(GameObject btn)
	{
		if (!clickflag && Planelist.Count != 1)
		{
			clickflag = true;
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			CurIndex = (CurIndex + 1 + Planelist.Count) % Planelist.Count;
			CurPlane = Planelist[CurIndex];
			selectpoint.parent = points[CurIndex].transform;
			selectpoint.localPosition = Vector3.zero;
			showplane();
		}
	}

	public void OnClickrewardBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		if (rewardhint.enabled)
		{
			rewardhint.enabled = false;
		}
		disable();
		MenuUIController.instance.enable_rewardui();
	}

	public void OnClickjunxianBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		MenuUIController.instance.enable_junxianui();
	}

    public void OnClickPrivacyBtn(GameObject btn)
    {
        MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);

        MenuUIController.instance.enable_privacyui();
		GameObject.Find("Canvas").transform.Find("PrivacyPanel").gameObject.SetActive(true);
    }

    public void OnClickmoregameBtn(GameObject btn)
	{
		MenuUIController.instance.enable_hitinfoui(hitinfoid.customisation, "敬请期待");
		/* if (MenuPlayerprefs.instance.firstshowgame == 0)
		{
			MenuPlayerprefs.instance.Setfirstmoregame();
		}
		if (moregamehint.enabled)
		{
			moregamehint.enabled = false;
		}
		Platform.ShowMoreGames(); */
	}

	public void OnClicksaleBtn(GameObject btn)
	{
		if (btn != null)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		}
		/* if (MenuPlayerprefs.instance.saleid == 0)
		{
			MenuUIController.instance.enable_saleui1();
		} */
		/* else if (MenuPlayerprefs.instance.saleid == 2)
		{
			MenuUIController.instance.enable_saleui2();
		} */
	}

	public void sale_buyok(int id)
	{
		switch (id)
		{
			case 12:
				MenuUIController.instance.saleui1.disable();
				if (MenuPlayerprefs.instance.curgamelevel >= 17)
				{
					//NGUITools.SetActive(saleobj, state: true);
					showsaletime = true;
					caltime = saletime;
				}
				else
				{
					showsaletime = false;
					//NGUITools.SetActive(saleobj, state: false);
				}
				break;
			case 13:
				MenuUIController.instance.saleui2.disable();
				showsaletime = false;
				//NGUITools.SetActive(saleobj, state: false);
				break;
		}
	}

	public void OnClickdailyBtn(GameObject btn)
	{
		if (Platform.GetServerTime() > 0 || Platform.GetServerTime() == -1)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
			MenuUIController.instance.enable_dailybonusui();
		}
		else
		{
			Platform.ToastLong("Network failure, please check the network!");
		}
	}

	public void OnClickboxbtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		if (rewardhint.enabled)
		{
			rewardhint.enabled = false;
		}
		disable();
		MenuUIController.instance.enable_rewardui(0);
	}

	public void OnClicksetBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		MenuUIController.instance.enable_optionui();
	}

	public void OnClickstoreBtn(GameObject btn)
	{
		if (btn != null)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		}
		disable();
		MenuUIController.instance.enable_garage(CurPlane);
	}

	public void OnClickbattleBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		disable();
		MenuUIController.instance.enable_battleui();
	}

	public void OnClickchallengeBtn(GameObject btn)
	{
		if (btn != null)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		}
		disable();
		MenuUIController.instance.enable_challengeui(chahintid);
		if (challengehint.enabled)
		{
			challengehint.enabled = false;
		}
	}

	public void enable()
	{
		if (uiobj == null)
		{
			uiobj = base.gameObject;
		}
		NGUITools.SetActive(uiobj, state: true);
		init_ui_info();
	}

	public void disable()
	{
		NGUITools.SetActive(uiobj, state: false);
		MenuPlayerprefs.instance.savestartplane(CurPlane);
		SetGlobalPlaneInfo();
	}

	public void SetGlobalPlaneInfo()
	{
		GlobalInf.CurPlayerAtt.planeindex = CurPlane;
		MenuDataController.instance.SetGlobalWeapon(CurPlane);
	}
}
